package com.aptech.oas.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.hibernate.Session;

import com.aptech.oas.dao.EmailDao;
import com.aptech.oas.dao.EmailDaoImpl;
import com.aptech.oas.dao.UserDao;
import com.aptech.oas.dao.UserDaoImpl;
import com.aptech.oas.dto.User1;
import com.aptech.oas.model.Email;
import com.aptech.oas.model.User;
import com.aptech.oas.util.SessionFactoryUtil;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class EmailAction extends ActionSupport {

	// 用户点击邮件标题时，进入邮件详情页，eid为该邮件的id; 移动邮件到垃圾邮箱，也用到eid
	private Long eid;
	//1、 用户在收件箱里 删除邮件操作，当confirm为true时，用户确认将邮件放到垃圾邮箱
	//2、在垃圾邮箱里，有还原、删除操作，confirm为true时，删除垃圾邮件；confirm为false时，还原垃圾邮件为正常邮件
	private Boolean confirm;

	/**
	 * 写邮件时，form表单提交过来的 收件人工号、邮件标题、邮件内容、附件
	 */
	private String workNo;
	private String title;
	private String details;
	private File attachment;
	/**
	 * attachmentFileName 附件的文件名 ,
	 * attachmentContentType 附件的文本类型 这两个都是struts自动填充的
	 */
	private String attachmentFileName;
	private String attachmentContentType;

	private EmailDao emailDao = new EmailDaoImpl();

	private UserDao userDao = new UserDaoImpl();

	public String writeEmail() {
		List<User> u = userDao.selectAllUser();
		HttpServletRequest request = ServletActionContext.getRequest();
		List<User1> users = new ArrayList<User1>();
		for (User user : u) {
			User1 user1 = new User1();
			user1.setUsername(user.getUsername());
			user1.setWorkNo(user.getWorkNo());
			users.add(user1);
		}
		request.setAttribute("users", users);
		return SUCCESS;
	}

	public String sendEmail() throws IOException {
		if (workNo == null || title == null) {
			addFieldError("邮件标题有误", "标题不符合");
			return INPUT;
		}
		String userWorkNo = (String) ActionContext.getContext().getSession().get("loginUser");
		User receiver = new User();
		receiver.setWorkNo(workNo);
		User sender = new User();
		sender.setWorkNo(userWorkNo);

		Email email = new Email();
		email.setTitle(title);
		email.setDetails(details);
		email.setSendTime(new Date());
		email.setSender(sender);
		email.setReceiver(receiver);
		Session session = SessionFactoryUtil.get().openSession();
		session.beginTransaction();
		session.save(email);
		session.getTransaction().commit();

		long eid = email.getEid();
		System.out.println(eid);

		if (attachment != null && attachmentFileName != null) {
			String parent = "g:/attachment/" + eid;
			File f1 = new File(parent);
			f1.mkdirs();
			InputStream is = new FileInputStream(attachment);
			OutputStream os = new FileOutputStream(new File(parent, attachmentFileName));
			byte[] buffer = new byte[1024];
			int length = 0;
			while (-1 != (length = is.read(buffer))) {
				os.write(buffer, 0, length);
			}
			is.close();
			os.close();

			email.setAttachment(parent);
			session.beginTransaction();
			session.update(email);
			session.getTransaction().commit();
		}
		session.close();
		ServletActionContext.getRequest().setAttribute("attachment", attachment);
		return SUCCESS;
	}

	public String receiveEmail() {
		String receiverWorkNo=(String) ActionContext.getContext().getSession().get("loginUser");
		List<Email> emails = emailDao.receiveEmail(receiverWorkNo,false);
		HttpServletRequest request = ServletActionContext.getRequest();
		request.setAttribute("emails", emails);
		return SUCCESS;
	}
	public String emailDetails() {
		if (eid != 0) {
			Email email = emailDao.selectEmailById(eid);
			email.setIsRead(true);
			emailDao.updateEmail(email);
			HttpServletRequest request = ServletActionContext.getRequest();
			request.setAttribute("email", email);
			return SUCCESS;
		}
		addFieldError("eid error", "网页跳转失败");
		return INPUT;
	}
	public String removeEmail() {
		if (confirm && eid != null ) {
			Email email=emailDao.selectEmailById(eid);
			email.setStatus(confirm);
			emailDao.updateEmail(email);
			return SUCCESS;
		}
		addFieldError("parameter error", "操作失败");
		return INPUT;
	}

	
	
	public String garbageCan() {
		String receiverWorkNo=(String) ActionContext.getContext().getSession().get("loginUser");
		List<Email> emails = emailDao.receiveEmail(receiverWorkNo,true);
		HttpServletRequest request = ServletActionContext.getRequest();
		request.setAttribute("emails", emails);
		return SUCCESS;
	}
	//处理 垃圾邮箱里的还原和删除操作
	public String spam(){
		if(confirm!=null&&eid!=null){
			if(confirm){
				Integer result=emailDao.deleteEmail(eid);
				if(result==null ||result < 1){
					addFieldError("邮件删除失败","邮件删除失败");
					return INPUT;
				}
			}else{
				Email email=emailDao.selectEmailById(eid);
				email.setStatus(confirm);
				emailDao.updateEmail(email);
			}
			return SUCCESS;
		}
		addFieldError("还原或删除失败", "还原或删除失败");
		return INPUT;
	}


	public String getWorkNo() {
		return workNo;
	}

	public void setWorkNo(String workNo) {
		this.workNo = workNo;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getDetails() {
		return details;
	}

	public void setDetails(String details) {
		this.details = details;
	}

	public UserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	public EmailDao getEmailDao() {
		return emailDao;
	}

	public void setEmailDao(EmailDao emailDao) {
		this.emailDao = emailDao;
	}

	public long getEid() {
		return eid;
	}

	public void setEid(long eid) {
		this.eid = eid;
	}

	public void setEid(Long eid) {
		this.eid = eid;
	}

	public Boolean getConfirm() {
		return confirm;
	}

	public void setConfirm(Boolean confirm) {
		this.confirm = confirm;
	}

	public File getAttachment() {
		return attachment;
	}

	public void setAttachment(File attachment) {
		this.attachment = attachment;
	}

	public String getAttachmentFileName() {
		return attachmentFileName;
	}

	public void setAttachmentFileName(String attachmentFileName) {
		this.attachmentFileName = attachmentFileName;
	}

	public String getAttachmentContentType() {
		return attachmentContentType;
	}

	public void setAttachmentContentType(String attachmentContentType) {
		this.attachmentContentType = attachmentContentType;
	}

	

}
